package ar.edu.itba.it.paw.web.product;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.itba.it.paw.domain.Auction;
import ar.edu.itba.it.paw.domain.Category;
import ar.edu.itba.it.paw.domain.EntityModel;
import ar.edu.itba.it.paw.domain.OrderProducts;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.repo.CategoryRepo;
import ar.edu.itba.it.paw.repo.ProductRepo;
import ar.edu.itba.it.paw.web.MercadoLibre;
import ar.edu.itba.it.paw.web.base.BasePage;

public class OrderProductPage extends BasePage{

	private static final long _1L = 1L;
	@SpringBean
	private ProductRepo productRepo;
	@SpringBean
	private CategoryRepo categoryRepo;
	private IModel<Category> categoryModel;
	
	
	public OrderProductPage(Category category,final String option, final String order){
		
		categoryModel = new EntityModel<Category>(Category.class,category);
		
		setDefaultModel(new CompoundPropertyModel<Category>(categoryModel));
		
		if(category == null){
			add(new Label("chosenCategory","All"));
		}else{
			add(new Label("chosenCategory",new PropertyModel<Category>(getDefaultModel(),"name"))).add(new SimpleAttributeModifier("style", ""));
		}
		
		add(new FeedbackPanel("feedback").add(new SimpleAttributeModifier("style", "color: #FF0000")));
		Form<OrderProductPage> categoryForm = new Form<OrderProductPage>("categoryForm", new CompoundPropertyModel<OrderProductPage>(this)) {

		};
		IModel<List<Category>> categoriesModel = new LoadableDetachableModel<List<Category>>() {
			@Override
			protected List<Category> load() {
				//TODO:Agregar opcion, todas
				List<Category> aux = categoryRepo.getCategories();
				aux.add(new Category(0,"Todos"));
				return aux;
			}
		};
		categoryForm.add(new DropDownChoice<Category>("formCategory", categoryModel,categoriesModel,new ChoiceRenderer<Category>("name","id")).setRequired(true));
		categoryForm.add(new Button("listar", new ResourceModel("listar")));
		add(categoryForm);
		
		add(new Link<OrderProductPage>("nameLink", new CompoundPropertyModel<OrderProductPage>(this)) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				Category cat = ((Category)OrderProductPage.this.getDefaultModelObject());
				if(!option.equals("nombre")){
					setResponsePage(new OrderProductPage(cat, "nombre", "asc"));
				}else{
					if(order.equals("asc")){
						setResponsePage(new OrderProductPage(cat, "nombre", "desc"));
					}else{
						setResponsePage(new OrderProductPage(cat, "nombre", "asc"));
					}
				}
			}
		});
		add(new Link<OrderProductPage>("priceLink") {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				Category cat = ((Category)OrderProductPage.this.getDefaultModelObject());
				if(!option.equals("precio")){
					setResponsePage(new OrderProductPage(cat, "precio", "asc"));
				}else{
					if(order.equals("asc")){
						setResponsePage(new OrderProductPage(cat, "precio", "desc"));
					}else{
						setResponsePage(new OrderProductPage(cat, "precio", "asc"));
					}
				}
			}
		});
		add(new Link<OrderProductPage>("dateLink") {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				Category cat = ((Category)OrderProductPage.this.getDefaultModelObject());
				if(!option.equals("fecha")){
					setResponsePage(new OrderProductPage(cat, "fecha", "asc"));
				}else{
					if(order.equals("asc")){
						setResponsePage(new OrderProductPage(cat, "fecha", "desc"));
					}else{
						setResponsePage(new OrderProductPage(cat, "fecha", "asc"));
					}
				}
			}
		});
		IModel<List<Product>> productsModel = new LoadableDetachableModel<List<Product>>() {
			@Override
			protected List<Product> load() {
//				List<Product> list = new ArrayList<Product>(((Product)getDefaultModelObject()).getOwner().getProducts_set()); 
//				list.remove(((Product)getDefaultModelObject()));
//				return list;
				OrderProducts orderInfo = null;
				boolean ascending;
				if(order.equals("asc")){
					ascending = true;
				}else if(order.equals("desc")){
					ascending = false;
				}else{
					return null;
				}
				if(option.equals("nombre")){
					orderInfo = new OrderProducts("name",ascending,0);
				}else if(option.equals("precio")){
					orderInfo = new OrderProducts("price",ascending,0);
				}else if(option.equals("fecha")){
					orderInfo = new OrderProducts("release_date",ascending,0);
				}else{
					return null;
				}
				List<Product> prod_list = null;
				Category categ = ((Category)getDefaultModelObject());
				if(categ == null || categ.getId() == 0){
					prod_list = productRepo.getAllProducts(orderInfo);
				}else if(!((Category)getDefaultModelObject()).getName().equals("Todos")){
					prod_list = productRepo.getAllProducts(orderInfo,((Category)getDefaultModelObject()));
				}
				return prod_list;
			}
		};


		add(new PropertyListView<Product>("categoryProducts", productsModel) {

			@Override
			protected void populateItem(ListItem<Product> item) {
				item.add(new Label("name",new PropertyModel<Product>(item.getModel(), "name")).add(new SimpleAttributeModifier("style", "color: #0099FF")));
				item.add(new Label("price").add(new SimpleAttributeModifier("style", "")));
				item.add(new Label("date", new PropertyModel<Product>(item.getModel(),"release_date")).add(new SimpleAttributeModifier("style","color: #FF00FF")));
				item.add(new Link<Product>("detail", item.getModel()) {
					@Override
					public void onClick() {
						setResponsePage(new ProductDetailPage(getModelObject()));
					}
				}.add(new Image("detail_image", MercadoLibre.DETAIL_ICON).add(new SimpleAttributeModifier("height","15px"))));
			}

		});
	}

}
